﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CopySniffer.UI.Core.Views;
using CopySniffer.UI.Core.Controlers;
using WeifenLuo.WinFormsUI.Docking;
using CopySniffer.UI.Core;

namespace CopySniffer.UI.Views.WindowsForms
{
    public partial class MainView : BaseForm, IMainView
    {
        public MainView()
        {
            InitializeComponent();
        }

        #region IView<MainControler> Members

        public MainControler Controler
        {
            get;
            set;
        }

        public void DockEditor(IEditorView editorView)
        {
            EditorView editor = (EditorView)editorView;
            editor.Show(dockPanel, DockState.Document);
        }

        public void SearchCopies()
        {
            IEditorView editorView = GetActiveEditorView();
            if (editorView != null)
            {
                Controler.SearchCopies(editorView.Controler);
            }
        }

        private IEditorView GetActiveEditorView()
        {
            IEditorView view = this.ActiveMdiChild as IEditorView;
            return view;
        }

        #endregion

        private void MainView_FormClosing(object sender, FormClosingEventArgs e)
        {
            e.Cancel = ! Controler.AllowClose();
        }

        private void MainView_FormClosed(object sender, FormClosedEventArgs e)
        {
            Controler.Close();
        }

        private void newToolStripButton_Click(object sender, EventArgs e)
        {
            Controler.CreateFile();
        }

        private void openToolStripButton_Click(object sender, EventArgs e)
        {
            if (String.IsNullOrEmpty(openFileDialog.Filter))
            {
                openFileDialog.Filter = Controler.GetOpenFileFilter();
            }

            if (openFileDialog.ShowDialog() == DialogResult.OK)
            {
                Controler.LoadFile(openFileDialog.FileName);
            }
        }

        private void lookForCopiesToolStripButton_Click(object sender, EventArgs e)
        {
            // TODO: put this in the adequate place later on
            SearchCopies();
        }

        private void MainView_Load(object sender, EventArgs e)
        {

        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (Controler.AllowClose())
            {
                this.Close();
                Controler.Close();
            }
        }

        private void toolStripButton2_Click(object sender, EventArgs e)
        {
            Controler.ShowAboutBox();
        }

        private void cutAction1_Execute(object sender, EventArgs e)
        {
            DoEditorAction(editor => editor.Cut());
        }

        private void DoEditorAction(Action<IEditorView> action)
        {
            var editorView = GetActiveEditorView();
            if (editorView != null)
            {
                action(editorView);
            }
        }

        private void pasteAction1_Execute(object sender, EventArgs e)
        {
            DoEditorAction(editor => editor.Paste());
        }

        private void copyAction1_Execute(object sender, EventArgs e)
        {
            DoEditorAction(editor => editor.Copy());
        }

        private void selectAllAction_Execute(object sender, EventArgs e)
        {
            DoEditorAction(editor => editor.SelectAll());
        }

        #region IMainView Members


        public void RemoveEditor(EditorControler editor)
        {
            // TODO: use this to implement a "Window" main menu item
        }

        public MainViewState State
        {
            get;
            set;
        }

        #endregion
    }
}
